package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Area;
import service.AreaService;
import service.factory.ServiceFactory;
import util.JsonUtil;
import dto.AreaDto;

//@WebServlet("/area/all")
public class GetAllAreaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private AreaService areaService;
       
    public GetAllAreaServlet() {
        super();
       areaService = ServiceFactory.getAreaServiceInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		String likeName = request.getParameter("likeName");
		List<AreaDto> areaDtos = new ArrayList<>();
		if (likeName!=null&&!"".equals(likeName)) {
			List<Area> areas = areaService.getAreasByLikeName(likeName);
			for (Area area : areas) {
				AreaDto areaDto = new AreaDto(area.getArea());
				areaDtos.add(areaDto);
			}
		}
		PrintWriter writer = response.getWriter();
		writer.write(JsonUtil.toJsonString(areaDtos));
		writer.close();
	}

	

}
